<template>
	<view class="content">
		<image class="logo"  size="200upx" src="../../static/icon/ui/user/coin.png"></image>
		<view>我的金币</view>
		<view class="text-area">
			<text class="title">{{number}}</text>
		</view>
		<view class="action-btn">
			<view class="button-s">
				<u-button type="primary" @click="recharge">充值</u-button>
			</view>
			<view class="button-s">
			<u-button @click="withdrawal">提现</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				number: '520'
			}
		},
		onShow() {
			// 监听选择返回的数据
			uni.$on("handleFun", res => {
			    var add = res.add;
			    // 清除监听
			    uni.$off('handleFun');
				this.number = parseFloat(this.number)+parseFloat(add);
			})
		},
		onLoad() {
			setTimeout(()=>{
				this.loadData();
			},1000);
		},
		methods: {
			loadData(){
				let token = this.isLogin();
				if (token) {
					uni.request({
						url: this.httpRoot + 'wallet/query',
						method: 'GET',
						header: {
							"token": token
						},
						data: {},
						success: res => {
							console.debug(res);
							console.debug(res.data);
							var code = 0;
							var data = {};
							var message = '';
							if ('code' in res.data) {
								code = res.data.code;
							}
							if ('message' in res.data) {
								message = res.data.message;
							}
							if ('data' in res.data) {
								data = res.data.data;
								// data = res.data.data.parseJSON(); //由JSON字符串转换为JSON对象
								// data = JSON.parse(res.data.data);
								console.debug('数据：');
								console.debug(data);
								if ('money' in data) {
									this.number=data.money;
								}
							} else {
								uni.showToast({
									title: '服务器错误',
									icon: 'none'
								});
							}
							if (message != '') {
								uni.showToast({
									title: message,
									icon: 'none'
								});
							}
						},
						fail: () => {
							uni.showModal({
								title: '请求数据失败'
							});
						},
						complete: () => {
				
						}
					});
				} else {
					uni.showToast({
						title: '请先登录',
						icon: 'none'
					});
				}
			},
			withdrawal(){
				console.debug('提现');
				if(this.number>0){
					
					let token = this.isLogin();
					if (token) {
						uni.request({
							url: this.httpRoot + 'wallet/withdrawal',
							method: 'POST',
							header: {
								"token": token
							},
							data: {},
							success: res => {
								console.debug(res);
								console.debug(res.data);
								var code = 0;
								var data = {};
								var message = '';
								if ('code' in res.data) {
									code = res.data.code;
								}
								if ('message' in res.data) {
									message = res.data.message;
								}
								if ('data' in res.data) {
									data = res.data.data;
								} 
								if(code==200){
									setTimeout(()=>{uni.showToast({
										title:'全部提现成功'
									});
									this.number = 0;},1000);
								}
								if (message != '') {
									uni.showToast({
										title: message,
										icon: 'none'
									});
								}
							},
							fail: () => {
								uni.showModal({
									title: '请求数据失败'
								});
							},
							complete: () => {
					
							}
						});
					} else {
						uni.showToast({
							title: '请先登录',
							icon: 'none'
						});
					}
				}else{
					uni.showToast({
						title:'失败，余额不足',
						icon:'none'
					});
				}
			},
			recharge(){
				console.debug('充值');
				uni.navigateTo({
					url:'../wallet_recharge/wallet_recharge'
				});
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 90upx;
	}
	
	.button-s{
		width: 100%;
		padding: 20rpx;
	}
	.action-btn{
		position: fixed;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		width: 50%;
		bottom: 200upx;
	}
</style>
